package com.sl.plugin.step.childflow;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.sl.core.domain.dto.JsonMappingFieldDefine;
import com.sl.core.utils.JsonUtil;
import com.sl.core.engine.ProcessConfig;
import com.sl.core.engine.ProcessEngine;
import com.sl.core.engine.loader.DefaultProcessSourceFileLoader;
import com.sl.core.engine.loader.ProcessSourceFileLoader;
import com.sl.core.engine.meta.ProcessCompDescriptorService;
import com.sl.core.engine.meta.impl.AbstractProcessCompDescriptor;
import com.sl.core.engine.meta.row.ProcessRowDescriptor;
import com.sl.core.engine.transfer.MappingFiledDefineToJoltDslTransfer;
import com.sl.core.engine.utils.BeanUtil;
import lombok.Getter;

import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

//@Comp(id = "ManyToOne", name = "多行转一行", desc = "多行转一行", groupName = "转换")
@Getter
public class ChildFlowCompDescriptor extends AbstractProcessCompDescriptor<
        ChildFlowComp, ChildFlowCompContext> implements ProcessCompDescriptorService {

    private ProcessConfig processConfig;

    @Override
    public void loadDescriptor(Map<String, Object> map) {
        Map<String, Object> maps = BeanUtil.getMap(map, "config.output");
        List<Map<String, Object>> params = getListMapAttr(maps, "params");
        //找到对象关系
        List<JsonMappingFieldDefine> jsonMappingFieldDefine = JsonUtil.toBean(params, new TypeReference<List<JsonMappingFieldDefine>>() {
        });

        if (CollUtil.isNotEmpty(jsonMappingFieldDefine)) {
            //转移到了映射
            String transfer = MappingFiledDefineToJoltDslTransfer.transfer(jsonMappingFieldDefine);
            putAttr("jolt", transfer);
        }

        ProcessConfig processConfig = getProcessConfig();
        if(ObjectUtil.isNull(processConfig)){
            return;
        }

        String s = processConfig.getXml();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(s.getBytes(Charset.defaultCharset()));

        Map<String, Object> processEventInputDefine = ProcessEngine.INSTANCE.getProcessEventOutputDefine(byteArrayInputStream);


        ProcessRowDescriptor processRowDescriptor = buildResultProcessRowDescriptor(processEventInputDefine);
        setResultRowDescriptor(processRowDescriptor);
    }

    public ProcessConfig getProcessConfig() {
        if(ObjectUtil.isNotNull(processConfig)){
            return processConfig;
        }
        String groupId = getStrAttr("config.group.groupId");
        String processVersionId = getStrAttr("config.process.processVersionId");
        String processId = getStrAttr("config.process.processId");
        String processVersion = getStrAttr("config.process.processVersion");

        ProcessSourceFileLoader instance = DefaultProcessSourceFileLoader.INSTANCE;
        if (ObjectUtil.isNull(instance)) {
            return null;
        }
        processConfig = instance.load(groupId, processVersionId, processId, processVersion);
        return processConfig;
    }


    @Override
    public ProcessRowDescriptor getResultDescriptor(ProcessRowDescriptor rowDescriptor) {
        return getResultRowDescriptor();
    }
}
